<?php if(!defined('BASEPATH')) exit('No direct access to this file allowed');

/******************************************************************************\
 *  Render
 *
 *  Change Log
 *  0.1     -   Initial version of Render
 *
 *  Standard Tags:
 *  @author     Ruben Tan
 *  @copyright  Copyright (c) 2008 Rendervault
 *
 *  Developer Tags:
 *  @package    system
 *  @subpackage core
\******************************************************************************/
class Render
{
    protected $IB = null;
    // Dependencies   
    protected $_decimals = 6;

    /**--------------------------------------------------------------------------------------
     *  Render constructor
     *
     *  @author     Ruben Tan
     *  
     *  @access     protected
     *  @param      type        description
     *  @return     type        description
     */
    public function __construct( $IB )
    { 
        $this->IB = $IB;
    }
    
    /**--------------------------------------------------------------------------------------
     *  Flushes the view
     *
     *  @author     Ruben Tan
     *  
     *  @access     protected
     *  @param      type        description
     *  @return     type        description
     */
    public function flush_view( $path, $params = NULL )
    {
        if(!file_exists($path))
        {
            new Exception( realpath($path) . ' not found' );
        }
        
        ob_start();
        
        if($params != NULL && is_array( $params ))
        {
            extract( $params );
        }
        
        include_once ( $path );
        
        $output = ob_get_contents();
        
        if (isset($this->IB->profiler) && $this->IB->profiler->get_enabled())
        {
            $profile_data = $this->IB->profiler->get_profile_data();
            if (preg_match("|</body>.*?</html>|is", $output))
            {
                $output  = str_replace("|</body>.*?</html>|is", '', $output);
                $output .= '<div id="profiler"><h1>Profiling Data</h1><table><tr><td>Profile Point</td><td>Elapsed (s)</td><td>Time (s)</td></tr>';
                foreach($profile_data as $datum)
                {
                    $output .= '<tr><td>'.$datum['name'].'</td><td>'.$datum['elapsed'].'</td><td>'.$datum['time'].'</td></tr>';
                }
                $output .= '<tr><td>Total:</td><td colspan="2">'.$this->IB->profiler->elapsed( ).'</td></tr></table>';
                $output .= '</div></body></html>';
            }
        }
        
        @ob_end_clean();
        
        echo $output;
    }
}